package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 分页查询铭感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(WmSensitiveDto dto) {

        // 校验参数
        dto.checkParam();

        // 分页查询
        IPage<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper = Wrappers.lambdaQuery(WmSensitive.class)
                .like(StringUtils.isNotBlank(dto.getName()), WmSensitive::getSensitives, dto.getName())
                .orderByDesc(WmSensitive::getCreatedTime);

        this.page(page, queryWrapper);

        // 封装结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }

    /**
     * 保存铭感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult add(WmSensitive wmSensitive) {

        // 校验参数
        if (wmSensitive == null || StringUtils.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 敏感词不能重复
        WmSensitive sensitive = this.getOne(Wrappers.lambdaQuery(WmSensitive.class)
                .eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (sensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"敏感词名称重复");
        }

        // 保存数据
        wmSensitive.setCreatedTime(new Date());

        this.save(wmSensitive);

        return ResponseResult.okResult("敏感词保存数据成功");
    }

    /**
     * 更新敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult modify(WmSensitive wmSensitive) {

        // 校验参数
        if (wmSensitive == null || StringUtils.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 修改的敏感词不能重复，修改的名字可以和当前名字相同
        // 查询所有的敏感词
        List<WmSensitive> list = this.list();
        // 从数据中删除当前敏感词
        List<String> sensitiveList = list.stream()
                .filter(param -> !param.getId().equals(wmSensitive.getId()))
                .map(WmSensitive::getSensitives)
                .collect(Collectors.toList());

        if (sensitiveList.contains(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "修改的敏感词不能重复");
        }
        // 修改数据
        this.updateById(wmSensitive);

        return ResponseResult.okResult("修改数据成功!");
    }

    /**
     * 根据id，删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {

        // 校验参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 根据id查询数据
        WmSensitive wmSensitive = this.getById(id);
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 根据id删除敏感词
        this.removeById(id);

        return ResponseResult.okResult("根据id删除数据成功!");
    }
}
